﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;

namespace Messenger.ViewModel
{
    public partial class AddViewModel : ObservableRecipient, IRecipient<ValueChangedMessage<bool>>
    {
        bool allowNew;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string _name;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string _class;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string _phone;

        [RelayCommand(CanExecute = nameof(CanAddNew))]
        void AddNew()
        {
            WeakReferenceMessenger.Default.Send(new ValueChangedMessage<string>($"{Name} {Class} {Phone}"));

        }

        bool CanAddNew() => allowNew;

        public override string ToString()
        {
            return $"{Name} {Class} {Phone}";
        }



        public void Receive(ValueChangedMessage<bool> message)
        {
            allowNew = message.Value;
            AddNewCommand.NotifyCanExecuteChanged();
        }
    }
}
